package org.dingyom.basics.service.impl;

import org.dingyom.basics.mapper.BaseMapper;
import org.dingyom.basics.query.BaseQuery;
import org.dingyom.basics.service.IBaseService;
import org.dingyom.basics.util.PageList;
import org.dingyom.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)//查询时间 只读
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    BaseMapper<T> mapper;
    @Override
    @Transactional//默认就是非只读 等同于@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void add(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void delete(Serializable id) {
        mapper.remove(id);
//        int i = 1/0;//测试事务回滚
    }

    @Override
    @Transactional
    public void modify(T t) {
        mapper.update(t);
    }

    @Override
    public T getOne(Serializable id) {
        return mapper.loadById(id);
    }

    @Override
    public List<T> getAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList getPage(BaseQuery query) {
        /*查询总数*/
        long total = mapper.loadCount(query);
        if(total<1){//总数没有 就返回基础的PageList
            return new PageList();
        }
        List<T> rows = mapper.loadData(query);
        return new PageList(total,rows);
    }

    @Override
    public void batchDel(List<Long> ids) {
        mapper.batchRemove(ids);
    }
}
